import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:proj_shop/pages/webview_page.dart';
import 'package:proj_shop/util/navigator_util.dart';
import 'package:proj_shop/widgets/grid_nav.dart';
import 'package:proj_shop/widgets/loading_container.dart';
import 'package:proj_shop/widgets/local_nav.dart';
import 'package:proj_shop/widgets/sales_box.dart';
import 'package:proj_shop/widgets/search_bar.dart';
import 'package:proj_shop/widgets/sub_nav.dart';

const APPBAR_SCROLL_OFFSET = 100;
const SEARCH_BAR_DEFAULT_TEXT = '网红打卡地 景点 酒店 美食';
String city = '西安市';

class Home1Page extends StatefulWidget {
  @override
  _Home1PageState createState() => _Home1PageState();
}

class _Home1PageState extends State<Home1Page>
    with AutomaticKeepAliveClientMixin {
  bool isLoading = true;
  double appBarAlpha = 0;
  List<String> bannerList = [
    "images/bg0.jpeg",
    "images/bg1.jpeg",
    "images/bg2.jpeg",
  ];

  List localNavi = [
    "images/bg0.jpeg",
    "images/bg1.jpeg",
    "images/bg2.jpeg",
    "images/bg1.jpeg",
    "images/bg2.jpeg",
  ];
  List subNavList = [
    "images/bg0.jpeg",
    "images/bg1.jpeg",
    "images/bg2.jpeg",
    "images/bg1.jpeg",
    "images/bg2.jpeg",
    "images/bg2.jpeg",
  ];

  Map salesBox = {};

  @override
  void initState() {
    Future.delayed(Duration(seconds: 2), () {
      setState(() {
        isLoading = false;
      });
    });
    super.initState();
  }

  Future<Null> _handleRefresh() {
    return Future.delayed(Duration(seconds: 2));
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      backgroundColor: Color(0xfff2f2f2),
      body: LoadingContainer(
        isLoading: false,
        child: Stack(
          children: [
            MediaQuery.removePadding(
              removeTop: true,
              context: context,
              child: RefreshIndicator(
                onRefresh: _handleRefresh,
                child: NotificationListener(
                  onNotification: (Notification notification) {
                    if (notification is ScrollNotification &&
                        notification.depth == 0) {
                      _onScroll(notification.metrics.pixels);
                    }
                    return false;
                  },
                  child: _listView(),
                ),
              ),
            ),
            _appBar(),
          ],
        ),
      ),
    );
  }

  // 判断滚动改变透明度
  void _onScroll(offset) {
    double alpha = offset / APPBAR_SCROLL_OFFSET;
    if (alpha < 0) {
      alpha = 0;
    } else if (alpha > 1) {
      alpha = 1;
    }
    setState(() {
      appBarAlpha = alpha;
    });
  }

  @override
  bool get wantKeepAlive => true;

  ListView _listView() {
    return ListView(
      children: [
        _banner,
        Padding(
          padding: const EdgeInsets.fromLTRB(7, 4, 7, 4),
          child: LocalNav(localNavList: localNavi),
        ),
        Padding(
          padding: const EdgeInsets.fromLTRB(7, 0, 7, 4),
          child: GridNav(),
        ),
        Padding(
          padding: const EdgeInsets.fromLTRB(7, 0, 7, 4),
          child: SubNav(
            subNavList: subNavList,
          ),
        ),
        Padding(
          padding: const EdgeInsets.fromLTRB(7, 0, 7, 4),
          child: SalesBox(
            salesBox: salesBox,
          ),
        ),
      ],
    );
  }

  Column _appBar() {
    return Column(
      children: [
        Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
                colors: [Color(0x66000000), Colors.transparent],
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter),
          ),
          child: Container(
            padding: EdgeInsets.fromLTRB(0, 20, 0, 0),
            height: 80,
            decoration: BoxDecoration(
              color: Color.fromARGB((appBarAlpha * 255).toInt(), 255, 255, 255),
            ),
            child: SearchBar(
              searchBarType: appBarAlpha > 0.2
                  ? SearchBarType.homeLight
                  : SearchBarType.home,
              inputBoxClick: () {},
              speakClick: () {},
              leftButtonClick: () {},
              defaultText: SEARCH_BAR_DEFAULT_TEXT,
              city: city,
            ),
          ),
        ),
        Container(
          height: appBarAlpha > 0.2 ? 0.5 : 0,
          decoration: BoxDecoration(
            boxShadow: [BoxShadow(color: Colors.black12, blurRadius: 0.5)],
          ),
        ),
      ],
    );
  }

  Widget get _banner {
    return Container(
      height: 160,
      child: Swiper(
        itemCount: bannerList.length,
        autoplay: true,
        loop: true,
        pagination: SwiperPagination(),
        onTap: (index) {
          NavigatorUtil.push(
            context,
            WebViewPage(title: '广告', urlString: 'http://www.baidu.com'),
          );
        },
        itemBuilder: (_, index) {
          return Image.asset(
            bannerList[index],
            fit: BoxFit.fill,
          );
        },
      ),
    );
  }
}
